package com.example.qx.lianxra;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.qx.R;
import com.example.qx.adapter.xpyyanzhengadapter;
import com.example.qx.database.data;
import com.example.qx.lei.leilianxr;
import com.example.qx.lei.zhangmi;
import com.example.qx.main;
import com.example.qx.zc;
import com.example.qx.zy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class xinpenyou extends AppCompatActivity {
private data data=new data();
private  List<Integer> yzzhanghao;
private EditText editText;
private Button xpytianjia,fanhuilxr;
private zhangmi zhangmi= com.example.qx.lei.zhangmi.getInstance();

    public xinpenyou() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xinpenyou);
       editText=findViewById(R.id.xpyzhanghao);
         xpytianjia=findViewById(R.id.xpytianjia);
         fanhuilxr=findViewById(R.id.fhlianxr);
          fanhuilxr.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent =new Intent(getApplication(), zy.class);
                  startActivity(intent);
              }
          });
         xpytianjia.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 String b=editText.getText().toString();
                 int c=Integer.parseInt(b);
                 int d= 0;
                 try {
                     d = data.sfcunzai(zhangmi.getZhang(),c);
                 } catch (SQLException e) {
                     throw new RuntimeException(e);
                 }
                 if(d==1){display1();}
                 else if(d==0){display0();}
                 else if(d==-1){
                     try {
                         data.insertpy(zhangmi.getZhang(),c);
                     } catch (SQLException e) {
                         throw new RuntimeException(e);
                     }
                     display_1();}
             }
         });
        final Connection[] connection = new Connection[1];
        final leilianxr[] leilianxr = new leilianxr[1];
        List<leilianxr> list =new ArrayList<>();
        CountDownLatch latch=new CountDownLatch(1);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {Class.forName("com.mysql.jdbc.Driver").newInstance();}
                catch (IllegalAccessException | ClassNotFoundException |
                       InstantiationException e) {
                    throw new RuntimeException(e);}
                try {
                    connection[0] = DriverManager.getConnection("jdbc:mysql://sh-cdb-9761pjk2.sql.tencentcdb.com:29081/youqi","root","asd123456");
                    yzzhanghao=data.getpy(zhangmi.getZhang(),0,connection[0]);


                    for(int a=0;a<yzzhanghao.size();a++){
                        leilianxr leilianxr= null;

                        leilianxr = data.getgrxx(yzzhanghao.get(a),connection[0]);

                        list.add(leilianxr);
                    }

                } catch (SQLException e) {throw new RuntimeException(e);}
                latch.countDown();
            }
        }).start();
        try{
            latch.await();
            connection[0].close();
        }
        catch (InterruptedException | SQLException e)
        {e.printStackTrace();}


           RecyclerView recyclerView=findViewById(R.id.yanzhengxx);
         xpyyanzhengadapter xpyyanzhengadapter =new xpyyanzhengadapter(this,list);
       recyclerView.setLayoutManager(new LinearLayoutManager(this));
          recyclerView.setAdapter(xpyyanzhengadapter);

    }
    public void display1(){
        AlertDialog.Builder builder=new AlertDialog.Builder(xinpenyou.this);
        builder.setMessage("已是好友！！！");
        AlertDialog diog=builder.create();
        diog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                diog.dismiss();
            }
        },1000);

    }
    public void display0(){
        AlertDialog.Builder builder=new AlertDialog.Builder(xinpenyou.this);
        builder.setMessage("等待验证！！！");
        AlertDialog diog=builder.create();
        diog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                diog.dismiss();
            }
        },1000);

    }
    public void display_1(){
        AlertDialog.Builder builder=new AlertDialog.Builder(xinpenyou.this);
        builder.setMessage("已发送添加信息");
        AlertDialog diog=builder.create();
        diog.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                diog.dismiss();
            }
        },1000);

    }
}